package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.AftersaleSubmitBO;
import com.cskaoyan.bean.bo.OrderListWxBO;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.AfterSaleDetailVO;
import com.cskaoyan.bean.vo.AfterSaleListWxVO;
import com.cskaoyan.mapper.MarketAftersaleMapper;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.service.AfterSaleWxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author chenxu
 * @since 2022/04/11 15:45
 */
@Service
public class AfterSaleWxServiceImpl  implements AfterSaleWxService {

    @Autowired
    MarketAftersaleMapper aftersaleMapper;

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;
    @Override
    public AfterSaleListWxVO list(OrderListWxBO afterSaleBO) {

        PageHelper.startPage(afterSaleBO.getPage(),afterSaleBO.getLimit());
        //1.获取订单售后状态；
        Integer afterStatus = afterSaleBO.getStatus();
        //转换成short
        short status =afterStatus.shortValue();
        //根据status（订单售后状态）查找对应的订单
        MarketOrderExample example = new MarketOrderExample();
        example.setOrderByClause("add_time desc");
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andAftersaleStatusEqualTo(status).andDeletedEqualTo(false);
        List<MarketOrder> marketOrders = orderMapper.selectByExample(example);

        //2.获取页码数
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
        int pages = pageInfo.getPages();
        int total = (int) pageInfo.getTotal();

        ArrayList<AfterSaleListWxVO.ListDTO> list = new ArrayList<>();

        //3.遍历marketOrder
        for (MarketOrder marketOrder : marketOrders) {
            //根据orderid查找获取aftersale的信息;
            MarketAftersaleExample example1 = new MarketAftersaleExample();
            MarketAftersaleExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andOrderIdEqualTo(marketOrder.getId());
            List<MarketAftersale> marketAftersales = aftersaleMapper.selectByExample(example1);
            //设订单id和申请售后id一一对应的；
            MarketAftersale marketAftersale = new MarketAftersale();
            if ((marketAftersales.size() != 0)){
             marketAftersale = marketAftersales.get(0);
            }

            //获取ordergoods的信息
            MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
            MarketOrderGoodsExample.Criteria criteria2 = orderGoodsExample.createCriteria();
            criteria2.andOrderIdEqualTo(marketOrder.getId());
            List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

            //第一层封装：listDTO
            AfterSaleListWxVO.ListDTO listDTO = new AfterSaleListWxVO.ListDTO();
            listDTO.setGoodsList(marketOrderGoods);
            listDTO.setAftersale(marketAftersale);

            list.add(listDTO);
        }
        AfterSaleListWxVO afterSaleListWxVO = new AfterSaleListWxVO(total, pages, afterSaleBO.getLimit(), afterSaleBO.getPage(), list);
        return afterSaleListWxVO;
    }

    @Override
    public AfterSaleDetailVO detail(Integer orderId) {
        //查找订单
        MarketOrder order = orderMapper.selectByPrimaryKey(orderId);

        //根据orderId查找订单商品
        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        AfterSaleDetailVO.OrderDTO orderDTO = new AfterSaleDetailVO.OrderDTO(
                order.getId(),order.getUserId(),order.getOrderSn(),order.getOrderStatus(),order.getAftersaleStatus(),
                order.getConsignee(),order.getMobile(),order.getAddress(),order.getMessage(),order.getGoodsPrice(),
                order.getFreightPrice(),order.getCouponPrice(),order.getIntegralPrice(),order.getGrouponPrice(),
                order.getOrderPrice(),order.getActualPrice(),order.getShipSn(),order.getShipChannel(),order.getShipTime(),
                order.getConfirmTime(),order.getComments(),order.getEndTime(),order.getAddTime(),order.getUpdateTime(),order.getDeleted());

        //根据orderId查找售后信息
        MarketAftersaleExample aftersaleExample = new MarketAftersaleExample();
        MarketAftersaleExample.Criteria criteria1 = aftersaleExample.createCriteria();
        criteria1.andOrderIdEqualTo(orderId);
        List<MarketAftersale> marketAftersales = aftersaleMapper.selectByExample(aftersaleExample);
        MarketAftersale marketAftersale = marketAftersales.get(0);

        //封装成
        AfterSaleDetailVO saleDetailVO = new AfterSaleDetailVO();
        saleDetailVO.setAftersale(marketAftersale);
        saleDetailVO.setOrder(orderDTO);
        saleDetailVO.setOrderGoods(marketOrderGoods);

        return saleDetailVO;
    }


    @Override
    public void submit(AftersaleSubmitBO submitBO) {
        // 售后编号;先获取当前时间的年月日，在随机生成一个6位数，进行拼接
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMMdd");
        String format = dateFormat.format(date);

        //7.随机生成一个六位数
        Random random = new Random();
        String result="";
        int length = 6;
        for (int i=0;i<length;i++) {
            result+=random.nextInt(10);
        }
        //获得订单编号：拼接时间和随机生成的六位数
        String aftersn = format + result;
        // todo userId = 1
        //status :1 表示用户已经申请
        MarketAftersale marketAftersale = new MarketAftersale(null,aftersn,submitBO.getOrderId(),1,
                submitBO.getType(),submitBO.getReason(),submitBO.getAmount(),submitBO.getPictures(),submitBO.getComment(),
                (short)1,null,new Date(),new Date(),false);

        aftersaleMapper.insertSelective(marketAftersale);

        //修改订单状态的售后状态aftersale为1:表示用户已经申请售后
        MarketOrder order = new MarketOrder();
        order.setAftersaleStatus((short)1);
        order.setId(submitBO.getOrderId());
        order.setOrderStatus((short)202);
        orderMapper.updateByPrimaryKeySelective(order);

    }
}
